package ai.people.netmon.warning.controller;

import ai.people.core.security.support.RequestHolderUtil;
import ai.people.netmon.framework.domain.warning.entity.WarningAccount;
import ai.people.netmon.framework.domain.warning.request.account.*;
import ai.people.netmon.framework.domain.warning.vo.AccountAlertDataVO;
import ai.people.netmon.framework.domain.warning.vo.JoinAnalysisAccountVO;
import ai.people.netmon.framework.domain.warning.vo.MessageContentVO;
import ai.people.netmon.framework.domain.warning.vo.WarningAccountVO;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.warning.service.WarningAccountService;
import ai.people.netmon.warning.service.WarningSpeechTemplateService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账户预警接口
 *
 * @author yuanqinglong
 * @date 2022/7/27 14:31
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/account")
public class WarningAccountController {


    private final WarningAccountService warningAccountServiceImpl;

    private final WarningSpeechTemplateService<AccountWarningInfoRequest, MessageContentVO> accountWarningServiceImpl;


    /**
     * 账号预警信息列表
     *
     * @param accountWarningRequest 账号预警请求
     * @return {@link Result}<{@link PageResponse}<{@link List}<{@link MessageContentVO}>>>
     */
    @PostMapping("/info/list")
    public Result<PageResponse<List<MessageContentVO>>> listAccountInfoWarning(@RequestBody AccountWarningInfoRequest accountWarningRequest) {
        PageResponse<MessageContentVO> listPageResponse = accountWarningServiceImpl.listMessages(accountWarningRequest);
        return ResultUtil.success(listPageResponse);
    }


    /**
     * 账号预警列表
     *
     * @param warningAccountRequest 警告帐户请求
     * @return {@link Result}<{@link PageResponse}<{@link List}<{@link AccountAlertDataVO}>>>
     */
    @PostMapping("/alert/list")
    public Result<PageResponse<List<AccountAlertDataVO>>> listAccountAlert(@RequestBody WarningAccountRequest warningAccountRequest) {
        return ResultUtil.success(warningAccountServiceImpl.listAccountAlert(warningAccountRequest));
    }


    /**
     * 重点账号导航页面
     *
     * @return {@link Result}<{@link PageResponse}<{@link List}<{@link WarningAccountVO}>>>
     */
    @PostMapping("/navigation/list")
    public Result<PageResponse<List<WarningAccountVO>>> keyAccountNavigationPage() {
        return ResultUtil.success(warningAccountServiceImpl.keyAccountNavigation(RequestHolderUtil.getUserId()));
    }


    /**
     * 重点账号列表（加入分析账号列表）
     *
     * @param accountJoinedListRequest 账户加入列表请求
     * @return {@link Result}<{@link ListResponse}<{@link JoinAnalysisAccountVO}>>
     */
    @PostMapping("/joined/list")
    public Result<PageResponse<JoinAnalysisAccountVO>> listJoinAnalysisAccount(@RequestBody AccountJoinedListRequest accountJoinedListRequest) {
        return ResultUtil.success(warningAccountServiceImpl.listJoinAnalysisAccount(accountJoinedListRequest));
    }


    /**
     * 添加警告帐户
     *
     * @param addWarningAccountRequest 添加警告帐户请求
     * @return {@link Result}<{@link Void}>
     */
    @PostMapping
    public Result<Void> addWarningAccount(@Validated @RequestBody AddWarningAccountRequest addWarningAccountRequest) throws InterruptedException {
        WarningAccount warningAccount = JSON.parseObject(JSON.toJSONString(addWarningAccountRequest), WarningAccount.class);
        warningAccountServiceImpl.addWarningAccount(warningAccount);
        return ResultUtil.success();
    }



    /**
     * 获取账户信息
     *
     * @param id id
     * @return {@link Result}<{@link WarningAccountVO}>
     */
    @GetMapping("/{id}")
    public Result<WarningAccountVO> getAccountBaseInfo(@PathVariable("id") Long id){
        return ResultUtil.success(warningAccountServiceImpl.getAccountBaseInfo(id));
    }




    /**
     * 更新注册信息
     *
     * @param accountRegisterInfoRequest 账户注册信息请求
     * @return {@link Result}<{@link Void}>
     */
    @PutMapping("/update/register/info")
    public Result<Void> updateRegisterInfo(@Validated @RequestBody AccountRegisterInfoRequest accountRegisterInfoRequest){
        WarningAccount warningAccount = JSON.parseObject(JSON.toJSONString(accountRegisterInfoRequest), WarningAccount.class);
        warningAccount.setId(accountRegisterInfoRequest.getAccountId());
        warningAccountServiceImpl.updateAccountInfo(warningAccount);
        return ResultUtil.success();
    }


    /**
     * 更新业务信息
     *
     * @param accountBusinessInfoRequest 账户业务信息请求
     * @return {@link Result}<{@link Void}>
     */
    @PutMapping("/update/business/info")
    public Result<Void> updateBusinessInfo(@Validated @RequestBody AccountBusinessInfoRequest accountBusinessInfoRequest){
        WarningAccount warningAccount = JSON.parseObject(JSON.toJSONString(accountBusinessInfoRequest), WarningAccount.class);
        warningAccount.setId(accountBusinessInfoRequest.getAccountId());
        warningAccountServiceImpl.updateAccountInfo(warningAccount);
        return ResultUtil.success();
    }


    /**
     * 更新研判信息
     *
     * @param accountJudgmentInfoRequest 账户信息判断请求
     * @return {@link Result}<{@link Void}>
     */
    @PutMapping("/update/judgment/info")
    public Result<Void> updateJudgmentInfo(@Validated @RequestBody AccountJudgmentInfoRequest accountJudgmentInfoRequest){
        WarningAccount warningAccount = JSON.parseObject(JSON.toJSONString(accountJudgmentInfoRequest), WarningAccount.class);
        warningAccount.setId(accountJudgmentInfoRequest.getAccountId());
        warningAccountServiceImpl.updateAccountInfo(warningAccount);
        return ResultUtil.success();
    }


    /**
     * 更新备注信息
     *
     * @param accountRemarkInfoRequest 账户备注信息请求
     * @return {@link Result}<{@link Void}>
     */
    @PutMapping("/update/remark/info")
    public Result<Void> updateRemarkInfo(@Validated @RequestBody AccountRemarkInfoRequest accountRemarkInfoRequest){
        WarningAccount warningAccount = JSON.parseObject(JSON.toJSONString(accountRemarkInfoRequest), WarningAccount.class);
        warningAccount.setId(accountRemarkInfoRequest.getAccountId());
        warningAccountServiceImpl.updateAccountInfo(warningAccount);
        return ResultUtil.success();
    }


    /**
     * 删除账号
     *
     * @param id id
     * @return {@link Result}<{@link Void}>
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteAccount(@PathVariable("id") Long id) {
        warningAccountServiceImpl.deleteAccount(id);
        return ResultUtil.success();
    }
}
